﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.VoteClassManage
{
	public partial class AddVoteClass : BasePage
	{
		private int? _pClassID;
		private bool _isLoadPClassID = false;
		/// <summary>
		/// 上级类别的标识。
		/// </summary>
		protected int? PClassID
		{
			get
			{
				if (!_isLoadPClassID)
				{
					string pClassID = Request.QueryString["PClassID"];
					if (string.IsNullOrEmpty(pClassID))
						_pClassID = null;
					else
						_pClassID = Convert.ToInt32(pClassID);
					_isLoadPClassID = true;
				}
				return _pClassID;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (RightValidate())
			{
				if (!IsPostBack)
					Page.DataBind();
			}
		}

		protected void BtSubmit_Click(object sender, EventArgs e)
		{
			if (RightValidate() && IsValid)
			{
				VoteClass info = UCVoteClass.Info;
				info.SiteId = SiteId.Value;
				info.PClassId = PClassID;
				info = VoteClasses.Insert(info);
				InsertComplete(info);
			}
		}

		/// <summary>
		/// 返回权限验证结果，如果验证失败则显示消息提示。
		/// </summary>
		/// <returns>布尔值代表权限验证是否通过。</returns>
		public bool RightValidate()
		{
			bool right = VoteClasses.GetInsertRight(new VoteClass { SiteId = SiteId.Value, PClassId = PClassID });
			if (!right)
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}
			return right;
		}

		/// <summary>
		/// 插入操作完成，显示消息提示。
		/// </summary>
		public void InsertComplete(VoteClass linkClass)
		{
			Messages.Add(new PageMessage(false, null, PageMessageType.Prompt, Res.OperationCompleteTitle, Res.OperationComplete_Insert, "ModifyVoteClass.aspx?ClassID=" + linkClass.ClassId, Res.OperationComplete_GoToModifyText));
		}
	}
}
